﻿using Capture.Hook;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;

namespace OsdTempGUI
{
    public partial class formProcessList : Form
    {
        List<Process> Processes = new List<Process>();
        TextBox FormMain_textboxProcessName;

        public formProcessList(TextBox textboxProcessName)
        {
            FormMain_textboxProcessName = textboxProcessName;
            InitializeComponent();            
        }

        private void buttonAnnulla_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void formProcessList_Load(object sender, EventArgs e)
        {
            fillProcessList();
        }

        private void fillProcessList()
        {
            Processes.Clear();
            listboxProcessList.Items.Clear();
            foreach (Process process in Process.GetProcesses())
            {
                Processes.Add(process);
            }
            Processes.Sort((x, y) => x.ProcessName.CompareTo(y.ProcessName));
            foreach (Process process in Processes)
            {
                listboxProcessList.Items.Add(process.ProcessName);
            }
        }

        private void buttonRefresh_Click(object sender, EventArgs e)        
        {            
            fillProcessList();
        }

        private void listboxProcessList_SelectedIndexChanged(object sender, EventArgs e)
        {
            Process selectedProcess = Processes[listboxProcessList.SelectedIndex];
            labelProcessDetails.Text = "ID: " + selectedProcess.Id.ToString();
            if (selectedProcess.MainWindowHandle == IntPtr.Zero)
                labelProcessDetails.Text += "\r\nMainWindowHandle: False";
            else
                labelProcessDetails.Text += "\r\nMainWindowHandle: True";

            if (HookManager.IsHooked(selectedProcess.Id))
                labelProcessDetails.Text += "\r\nHooked: True";
            else
                labelProcessDetails.Text += "\r\nHooked: False";            
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            FormMain_textboxProcessName.Text = Processes[listboxProcessList.SelectedIndex].ProcessName.ToString();
            this.Close();
        }       
    }
}
